package com.best.smart.logfile;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class CustomDialog extends DialogFragment {

    private static final String TAG = "CustomDialog";

    public static CustomDialog newInstance(String file) {
        CustomDialog dialog = new CustomDialog();
        Bundle args = new Bundle();
        args.putString("file", file);
        dialog.setArguments(args);
        return dialog;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
        // 布局填充器
        LayoutInflater inflater = requireActivity().getLayoutInflater();

        // 载入自定义布局
        View view = inflater.inflate(R.layout.dialog_custom, null);

        // 创建AlertDialog.Builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());

        // 使用自定义布局
        builder.setView(view);

        // 初始化视图
        Button positiveButton = view.findViewById(R.id.button_accept);
        Button negativeButton = view.findViewById(R.id.button_decline);
        String file = getArguments().getString("file");
        // 为按钮设置点击事件
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LogDetailActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("log",file);
                intent.putExtra("showEvent",0);
                getActivity().startActivity(intent);
            }
        });

        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LogDetailActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("log",file);
                intent.putExtra("showEvent",1);
                getActivity().startActivity(intent);
            }

        });

        // 创建AlertDialog对象
        return builder.create();
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        // 对话框消失时调用
    }

    // 定义按钮点击事件监听器接口
    public interface OnDialogButtonClickListener {
        void onAccept();
        void onDecline();
    }
}